package com.tycmc.signelectroniccontracts.tools.images;

import android.content.Context;
import android.os.Build;
import android.os.Environment;

import com.tycmc.signelectroniccontracts.tools.show.LogShow;

import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by gy on 2017/6/5.
 */

public class UrlPath {


    public static String urlPath = Environment.getExternalStorageDirectory().getAbsolutePath()+ "/signelectronicontracts";
    public static String voicePath = urlPath + "/voice/";
    public static String vidoPath = urlPath + "/vido/";
    public static String imagePath = urlPath + "/image/";
    public static String imageCashe = imagePath + "/cache/";
    public static String CrashLog = urlPath + "/cache/";
    public static String FilePath = urlPath + "/files/";
    public static String DownloadPath = urlPath + "/download/";
    public static List<String> strNoList = new ArrayList<>();

    public static void init() {
        exist(urlPath);
        exist(voicePath);
        exist(imagePath);
        exist(imageCashe);
        exist(CrashLog);
        exist(FilePath);
        exist(vidoPath);
        exist(DownloadPath);
    }

    public static String getTotalCacheSize(Context context) throws Exception {
        long cacheSize = getFolderSize(context.getCacheDir());
        cacheSize = cacheSize + getFolderSize(new File(urlPath));
        return getFormatSize(cacheSize);
    }

    public static long getFolderSize(File file) throws Exception {
        long size = 0;
        try {
            File[] fileList = file.listFiles();
            for (int i = 0; i < fileList.length; i++) {
                // 如果下面还有文件
                if (fileList[i].isDirectory()) {
                    size = size + getFolderSize(fileList[i]);
                } else {
                    size = size + fileList[i].length();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return size;
    }

    /**
     * 格式化单位
     *
     * @param size
     * @return
     */
    public static String getFormatSize(double size) {
        double kiloByte = size / 1024;
        if (kiloByte < 1) {
            return "0K";
        }

        double megaByte = kiloByte / 1024;
        if (megaByte < 1) {
            BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
            return result1.setScale(2, BigDecimal.ROUND_HALF_UP)
                    .toPlainString() + "K";
        }

        double gigaByte = megaByte / 1024;
        if (gigaByte < 1) {
            BigDecimal result2 = new BigDecimal(Double.toString(megaByte));
            return result2.setScale(2, BigDecimal.ROUND_HALF_UP)
                    .toPlainString() + "M";
        }
        double teraBytes = gigaByte / 1024;
        if (teraBytes < 1) {
            BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));
            return result3.setScale(2, BigDecimal.ROUND_HALF_UP)
                    .toPlainString() + "GB";
        }
        BigDecimal result4 = new BigDecimal(teraBytes);
        return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString()
                + "TB";
    }

    /**
     * 清除本应用所有数据库(/data/data/com.xxx.xxx/databases) * * @param context
     */
    public static void cleanDatabases(Context context) {
        deleteFilesByDirectory(new File("/data/data/"
                + context.getPackageName() + "/databases"));
    }

    public static void cleanInternalCache(Context context) {
        deleteAll(context.getCacheDir());
        deleteFilesByDirectory(context.getCacheDir());
        deleteFilesByDirectory(context.getFilesDir());
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            deleteFilesByDirectory(context.getExternalCacheDir());
        }
    }


    public static void clean() {
        deleteFilesByDirectory(new File(voicePath));
        deleteFilesByDirectory(new File(imageCashe));
        deleteFilesByDirectory(new File(imagePath));
        deleteFilesByDirectory(new File(CrashLog));
        deleteFilesByDirectory(new File(FilePath));
        deleteFilesByDirectory(new File(vidoPath));
        deleteFilesByDirectory(new File(DownloadPath));

    }

    public static void deleteAll(File file) {
        try {
            File[] fileList = file.listFiles();
            for (int i = 0; i < fileList.length; i++) {
                // 如果下面还有文件
                if (fileList[i].isDirectory()) {
                    deleteAll(fileList[i]);
                } else {
                    fileList[i].delete();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void deleteFilesByDirectory(File directory) {
        if (directory != null && directory.exists() && directory.isDirectory() && directory.listFiles() != null) {
            for (File item : directory.listFiles()) {
                item.delete();
            }
        }
    }

    public static void exist(String urlPath) {
        if (!new File(urlPath).exists()) {
            boolean mkdir = new File(urlPath).mkdirs();
        }

    }

}
